

const fs = require('fs');
const path = require('path');
const {BrowserWindow} = require('electron').remote;
const isDev = require('electron-is-dev');

const btn = this.document.querySelector('#btn');
const food = this.document.querySelector('#food');
const openRed = document.querySelector('#open-red');
const robotjsbtn =  document.querySelector('#robot-page');
const rtcAnswerPage = document.querySelector('#rtc-answer-page');
const rtcOfferPage = document.querySelector('#rtc-offer-page');


function openBrowser(opt) {
    let win = null;
    win = new BrowserWindow({
        width: 800 || opt.width,
        height: 600 || opt.height,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true
        }
    });
    console.log('robotjsbtn __dirname', path.join(__dirname, './robotjs.html'));
    win.loadFile(path.join(__dirname, opt.relativePath));
    // win.loadFile('./render/react-app/main/build/index.html');
    // if (isDev) {
    //     win.loadURL('http://192.168.3.5:3000');
    // }
    // else {
    // }
    win.on('close', () => {
        win = null;
    });
}   
window.onload = function() {
    btn.onclick = function() {
        fs.readFile(path.join(__dirname, '../render/food.txt'), 'utf8', (err, data) => {
            console.log('__dirname', __dirname);
            let text = '';
            if (err) {
                console.log('err', err);
                text = '读取出错' + err;
            }
            else {
                text = data;
            }
            food.innerHTML = text;
        });
    };

    openRed.onclick = function() {
        let redwin = null;
        redwin = new BrowserWindow({
            width: 800,
            height: 600,
            webPreferences: {
                nodeIntegration: true,
                enableRemoteModule: true
            }
        });
        console.log('openRed __dirname', __dirname);
        // redwin.loadFile('./template/red.html');
        if (isDev) {
            redwin.loadURL('http://192.168.3.5:3000');
        }
        else {
            redwin.loadFile('./render/react-app/main/build/index.html');
        }
        redwin.on('close', () => {
            redwin = null;
        });
    };

    robotjsbtn.onclick = () => {
        openBrowser({
            relativePath: './robotjs.html'
        });
    };

    rtcOfferPage.onclick = () => {
        openBrowser({
            relativePath: './rtc-stream-offer.html'
        });
    }

    rtcAnswerPage.onclick = () => {
        openBrowser({
            relativePath: './rtc-stream-answer.html'
        });
    }


};

